import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.model_selection import validation_curve
import matplotlib.pyplot as plt

# Load data from Excel file
file_path = 'Highest.xlsx'
features_df = pd.read_excel(file_path, sheet_name="Sheet1")
target_df = pd.read_excel(file_path, sheet_name="Sheet2")

# Assuming the target column name is "target"
target = target_df["target"]
features = features_df.values

# Split the data into training and testing sets (70% train, 30% test)
X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.2, random_state=42)

# Create the Decision Tree classifier
dt_classifier = DecisionTreeClassifier(random_state=42)

# Train the classifier on the training data
dt_classifier.fit(X_train, y_train)

# Make predictions on the test data
y_pred = dt_classifier.predict(X_test)

# Generate a classification report
class_names = target_df["target"].unique().tolist()
report = classification_report(y_test, y_pred, target_names=class_names)

print("Classification Report:")
print(report)

# Generate a confusion matrix
print("Confusion Matrix:")
print(confusion_matrix(y_test, y_pred))

param_range = np.arange(1, 200, 10)

# Create the Decision Tree classifier
dt_classifier = DecisionTreeClassifier(random_state=42)

# Calculate validation curve for the max depth of the tree
train_scores, test_scores = validation_curve(
    dt_classifier, features, target, param_name="max_depth", param_range=param_range,
    cv=5, scoring="accuracy", n_jobs=-1
)

# Calculate mean and standard deviation for train and test scores
train_mean = np.mean(train_scores, axis=1)
train_std = np.std(train_scores, axis=1)
test_mean = np.mean(test_scores, axis=1)
test_std = np.std(test_scores, axis=1)

# Plot the validation curve
plt.figure(figsize=(10, 6))
plt.title("Validation Curve for Decision Tree")
plt.xlabel("Max Depth of Tree")
plt.ylabel("Accuracy")
plt.ylim(0.0, 1.1)
plt.grid()

plt.plot(param_range, train_mean, label="Training Accuracy", color="blue")
plt.fill_between(param_range, train_mean - train_std, train_mean + train_std, alpha=0.2, color="blue")

plt.plot(param_range, test_mean, label="Cross-validation Accuracy", color="green")
plt.fill_between(param_range, test_mean - test_std, test_mean + test_std, alpha=0.2, color="green")

plt.legend(loc="best")
plt.show()
